#!/usr/bin/env python.
# -*- coding: utf-8 -*-

import wx
from core import Core
from core.event.mouseevent import MouseEvent
from core.ui.mainframe import MainFrame
import core.ui.mainframe as auipane
from core.ide.datamangager import DataMangager
from core.ui.toolsbar import ToolsBarManager
from core.ide.commandmanager import CommandManager
import subprocess
import sys 
 
 
class Extend():
    
    def __init__(self):
        self.id="CONSOLE"
        self.name="console"
        self.win=None
        
        
        
    def enable(self):
        #DataMangager().getData("Project","Open Last Project",True)
        self.ui=Panel()
        Core().getMainFrame().addPane(self.ui,auipane.PANE_BUTTOM)
        #self.ui.openProject(None)
        #ToolsBarManager().createToolsBar("Launcher","build","play")
        #self.win=Outline()
        #Core().getMainFrame().addPane(self.win,auipane.PANE_RIGHTPANEL)
    
    def disable(self):
        pass
        #del self.win  

class Panel(wx.Panel):
    def __init__(self):
        wx.Panel.__init__(self, MainFrame(), -1, style=wx.TAB_TRAVERSAL | wx.CLIP_CHILDREN | wx.FULL_REPAINT_ON_RESIZE | wx.NO_BORDER)
        self.SetAutoLayout(True)
        self.console=self.out = wx.TextCtrl(self, -1, '',style=wx.TE_MULTILINE|wx.TE_READONLY|wx.TE_RICH2)
        self.console.SetEditable(False)
        self.process = None
        box = wx.BoxSizer(wx.HORIZONTAL)
        self.SetSizer(box)
        box.Add(self.console, 1, wx.EXPAND)
        CommandManager().listener("execute",self.onExecute)
        self.Bind(wx.EVT_IDLE, self.onIdle)
        self.Bind(wx.EVT_END_PROCESS, self.onProcessEnded)
        self.proc=None

    
    def onExecute(self,command,base=None):
        #if not base:
        #    base="K:/projets/softwares/Druid/03_development/druid-ide/druid"
            #self.execCmd("cd "+base)
            
        if type(command) is list:
            self.execCmd(command,base)
        else:
            #self.execCmd(command,base)
            self.execCmd2(command,base)

    
    def execCmdNew(self,command,base=None):
        self.out.AppendText("1>"+ command+"\n")
        self.process = wx.Process(self)
        self.process.Redirect()
        pid = wx.Execute(command, wx.EXEC_ASYNC, self.process)
        #self.process.Open(command, wx.EXEC_ASYNC)

    def execCmd(self,command,base):
        self.out.AppendText("2>"+ command+"\n")
        self.proc = subprocess.Popen(command,cwd=base, shell=True, stdout=subprocess.PIPE)
        #while True:
        #    line = proc.stdout.readline()
        #    wx.Yield()
        #    self.out.AppendText(line)
        #    if not line: break
        #self.proc.wait()

    def execCmd2(self,command,base):
        child = subprocess.Popen(command, shell=True, stderr=subprocess.PIPE, stdout=subprocess.PIPE)
        complete = False
        while True:
            out = child.stdout.read()
            if out == '' and child.poll() != None:
                break
            if out != '':
                self.out.AppendText(out)
                
                
    def onIdle(self, evt):
        
        if self.process is not None:
            stream = self.process.GetInputStream()
            #print "allo"
            if stream and stream.CanRead():
                text = stream.read()
                self.out.AppendText(text)
                
        if self.proc:
            #wx.Yield()
            text = self.proc.stdout.readline()
            self.out.AppendText(text)

    def onProcessEnded(self, evt):
        stream = self.process.GetInputStream()
        if stream.CanRead():
            text = stream.read()
            self.out.AppendText(text)
        self.process.Destroy()
        self.process = None